package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.RecPOTbl;

public class RecPOTblPresenter extends AbstractPresenter<RecPOTbl> {
	final String SINGLEITEM = "RecPOTbl";
	final String MULTIITEM = "recPOTbls";
	
	public ArrayList<RecPOTbl> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public RecPOTbl tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public RecPOTbl createItemFromJSONObject(JSONObject jsonObject){
		RecPOTbl item = new RecPOTbl();
		try{
			
			if(!jsonObject.isNull("dueDate")){
				item.setDdueDate(timeFormat.parse(jsonObject.getString("dueDate")));
			} else {
				item.setDdueDate(null);
			}	
			
			item.setDonPO(jsonObject.getBoolean("onPO"));
			item.setDpartNumber(jsonObject.getInt("partNumber"));
			item.setDqty(jsonObject.getInt("qty"));
			item.setDrecomendedQty(jsonObject.getInt("recomendedQty"));
			item.setDvendorID(jsonObject.getInt("vendorID"));
			item.setDwarehouseID(jsonObject.getInt("warehouseID"));	
			
			if(!jsonObject.isNull("hop")){
				HopsPresenter presenter = new HopsPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("hop");
				item.setHop(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setHop(null);
			}	
			
			if(!jsonObject.isNull("malt")){
				MaltPresenter presenter = new MaltPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("malt");
				item.setMalt(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setMalt(null);
			}	
			
			if(!jsonObject.isNull("yeast")){
				YeastPresenter presenter = new YeastPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("yeast");
				item.setYeast(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setYeast(null);
			}	
			
			if(!jsonObject.isNull("supply")){
				SuppliesPresenter presenter = new SuppliesPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("supply");
				item.setSupply(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setSupply(null);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(RecPOTbl item){
		try{
			JSONObject json = new JSONObject();
			
			if(item.getDdueDate()!=null){
				json.put("dueDate", getDate(item.getDdueDate()));
			} else {
				json.put("dueDate", JSONObject.NULL);
			}
			
			json.put("onPO", item.isDonPO());
			json.put("partNumber", item.getDpartNumber());
			json.put("qty", item.getDqty());
			json.put("recomendedQty", item.getDrecomendedQty());
			json.put("vendorID", item.getDvendorID());
			json.put("warehouseID", item.getDwarehouseID());
			
			if(item.getHop()!=null){
				final HopsPresenter presenter = new HopsPresenter();
				json.put("hop", presenter.createJSON(item.getHop()));
			} else {
				json.put("hop", JSONObject.NULL);
			}
			
			if(item.getMalt()!=null){
				final MaltPresenter presenter = new MaltPresenter();
				json.put("malt", presenter.createJSON(item.getMalt()));
			} else {
				json.put("malt", JSONObject.NULL);
			}
			
			if(item.getYeast()!=null){
				final YeastPresenter presenter = new YeastPresenter();
				json.put("yeast", presenter.createJSON(item.getYeast()));
			} else {
				json.put("yeast", JSONObject.NULL);
			}
			
			if(item.getSupply()!=null){
				final SuppliesPresenter presenter = new SuppliesPresenter();
				json.put("supply", presenter.createJSON(item.getSupply()));
			} else {
				json.put("supply", JSONObject.NULL);
			}

			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public RecPOTbl getEmptyEntity(){
		RecPOTbl entity = new RecPOTbl();
		return entity;
	}
}
